﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Salon
{
    public partial class RegisterCars : Form
    {
        public RegisterCars()
        {
            InitializeComponent();
            dgvCarGrid.AutoGenerateColumns = false;
            Text = Forms.CAR_REGISTER;
        }

        private void RegisterCars_Load(object sender, EventArgs e)
        {
            FillTable();
        }


        private void FillTable()
        {
            BindingSource bs = new BindingSource();
            CarService us = new CarService();

            IList<Car> Cars = us.getCars(Global.getFilterList(this));

            foreach (Car Car in Cars)
                bs.Add(Car);

            dgvCarGrid.DataSource = bs;
        }


        private void dgvCarGrid_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (!Global.MainPage.checkIfFormOpen(Forms.CAR_FORM))
            {
                //-FormCar fc = new FormCar((Car)dgvCarGrid.Rows[e.RowIndex].DataBoundItem);
                //fc.MdiParent = Global.MainPage;
                //fc.Text = Forms.CAR_FORM;
                //fc.Show();
            }
            else
            {

            }
        }

        private void btnFilter_Click(object sender, EventArgs e)
        {
            FillTable();
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            foreach (IFilter filtr in Global.getFilterList(this))
            {
                filtr.CleanUp();
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            //-Global.MainPage.openNewCarForm(null);
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (dgvCarGrid.SelectedRows.Count != 1)
            {
                MessageBox.Show("Proszę zaznaczyć dokładnie jeden wiersz do edycji.");
                return;
            }

            //-Global.MainPage.openNewCarForm((Car)dgvCarGrid.SelectedRows[0].DataBoundItem);
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (dgvCarGrid.SelectedRows.Count < 1)
            {
                MessageBox.Show("Proszę zaznaczyć przynajmniej jeden wiersz do usunięcia.");
                return;
            }

            if (MessageBox.Show("Czy na pewno chcesz usunąć wszystkich zaznaczonych klientów?", "", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                CarService us = new CarService();
                for (int i = 0; i < dgvCarGrid.SelectedRows.Count; i++)
                    us.deleteCar((Car)dgvCarGrid.SelectedRows[i].DataBoundItem);
                FillTable();
            }
        }


    }
}
